<template>
  <el-dialog
    :title="!dataForm.no ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="厂家名称" prop="cpyname">
        <el-input v-model="dataForm.cpyname" placeholder="厂家名称"></el-input>
      </el-form-item>
      <el-form-item label="设备名称" prop="cncname">
        <el-input v-model="dataForm.cncname" placeholder="设备名称"></el-input>
      </el-form-item>
      <el-form-item label="设备类型" prop="cnctype">
        <el-input v-model="dataForm.cnctype" placeholder="设备类型"></el-input>
      </el-form-item>
      <el-form-item label="工作状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="工作状态"></el-input>
      </el-form-item>
      <el-form-item label="焊接电流" prop="welcur">
        <el-input v-model="dataForm.welcur" placeholder="焊接电流"></el-input>
      </el-form-item>
      <el-form-item label="焊接电压" prop="welvol">
        <el-input v-model="dataForm.welvol" placeholder="焊接电压"></el-input>
      </el-form-item>
      <el-form-item label="焊接电流设定" prop="welcurset">
        <el-input v-model="dataForm.welcurset" placeholder="焊接电流设定"></el-input>
      </el-form-item>
      <el-form-item label="焊接电压设定" prop="wevolset">
        <el-input v-model="dataForm.wevolset" placeholder="焊接电压设定"></el-input>
      </el-form-item>
      <el-form-item label="内部温度" prop="temperature">
        <el-input v-model="dataForm.temperature" placeholder="内部温度"></el-input>
      </el-form-item>
      <el-form-item label="内部湿度" prop="tumidity">
        <el-input v-model="dataForm.tumidity" placeholder="内部湿度"></el-input>
      </el-form-item>
      <el-form-item label="气流量" prop="rempos">
        <el-input v-model="dataForm.rempos" placeholder="气流量"></el-input>
      </el-form-item>
      <el-form-item label="送丝速度" prop="curpgm">
        <el-input v-model="dataForm.curpgm" placeholder="送丝速度"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        no: 0,
        cpyname: '',
        cncname: '',
        cnctype: '',
        status: '',
        welcur: '',
        welvol: '',
        welcurset: '',
        wevolset: '',
        temperature: '',
        tumidity: '',
        rempos: '',
        curpgm: ''
      },
      dataRule: {
        cpyname: [
          { required: true, message: '厂家名称不能为空', trigger: 'blur' }
        ],
        cncname: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        cnctype: [
          { required: true, message: '设备类型不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '工作状态不能为空', trigger: 'blur' }
        ],
        welcur: [
          { required: true, message: '焊接电流不能为空', trigger: 'blur' }
        ],
        welvol: [
          { required: true, message: '焊接电压不能为空', trigger: 'blur' }
        ],
        welcurset: [
          { required: true, message: '焊接电流设定不能为空', trigger: 'blur' }
        ],
        wevolset: [
          { required: true, message: '焊接电压设定不能为空', trigger: 'blur' }
        ],
        temperature: [
          { required: true, message: '内部温度不能为空', trigger: 'blur' }
        ],
        tumidity: [
          { required: true, message: '内部湿度不能为空', trigger: 'blur' }
        ],
        rempos: [
          { required: true, message: '气流量不能为空', trigger: 'blur' }
        ],
        curpgm: [
          { required: true, message: '送丝速度不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.no = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.no) {
          this.$http({
            url: this.$http.adornUrl(`/weldingmachine/weldingmachine/info/${this.dataForm.no}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.cpyname = data.weldingmachine.cpyname
              this.dataForm.cncname = data.weldingmachine.cncname
              this.dataForm.cnctype = data.weldingmachine.cnctype
              this.dataForm.status = data.weldingmachine.status
              this.dataForm.welcur = data.weldingmachine.welcur
              this.dataForm.welvol = data.weldingmachine.welvol
              this.dataForm.welcurset = data.weldingmachine.welcurset
              this.dataForm.wevolset = data.weldingmachine.wevolset
              this.dataForm.temperature = data.weldingmachine.temperature
              this.dataForm.tumidity = data.weldingmachine.tumidity
              this.dataForm.rempos = data.weldingmachine.rempos
              this.dataForm.curpgm = data.weldingmachine.curpgm
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/weldingmachine/weldingmachine/${!this.dataForm.no ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'no': this.dataForm.no || undefined,
              'cpyname': this.dataForm.cpyname,
              'cncname': this.dataForm.cncname,
              'cnctype': this.dataForm.cnctype,
              'status': this.dataForm.status,
              'welcur': this.dataForm.welcur,
              'welvol': this.dataForm.welvol,
              'welcurset': this.dataForm.welcurset,
              'wevolset': this.dataForm.wevolset,
              'temperature': this.dataForm.temperature,
              'tumidity': this.dataForm.tumidity,
              'rempos': this.dataForm.rempos,
              'curpgm': this.dataForm.curpgm
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
